package net.mbl.ide.tools;

import java.security.MessageDigest;

import net.mbl.ide.system.core.CorePlaceInfo;

import net.mbl.ide.tools.R;
import net.mbl.ide.utils.NetUtils;

import com.zxing.activity.CaptureActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.TextView;

public class BarCodeTestActivity extends Activity {
    /** Called when the activity is first created. */
	private TextView resultTextView;
	private TextView snStrEditText;
	private EditText ipStrEditText;
	private NumberPicker boxId_np;
	private NumberPicker slotId_np;
	private NumberPicker cpuId_np;
	private NumberPicker coreId_np;
	private OnValueChangeListener npOnValueChangeListener = new OnValueChangeListener(){


		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			// TODO Auto-generated method stub
			CorePlaceInfo cpi = new CorePlaceInfo();
			 cpi.setPlaceInfo(boxId_np.getValue(),slotId_np.getValue(),cpuId_np.getValue(),coreId_np.getValue());
			 cpi.calcIpFromPlaceInfo();
			String text = cpi.getIpStr();
			ipStrEditText.setText(text);
		}
		
	};
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        resultTextView = (TextView) this.findViewById(R.id.tv_scan_result);
        snStrEditText = (EditText) this.findViewById(R.id.et_sn_string);
        ipStrEditText = (EditText) this.findViewById(R.id.et_ip_string);
        Button scanBarCodeButton = (Button) this.findViewById(R.id.btn_scan_barcode);
        boxId_np = (NumberPicker) this.findViewById(R.id.np_box);
        slotId_np = (NumberPicker) this.findViewById(R.id.np_slot);
        cpuId_np = (NumberPicker) this.findViewById(R.id.np_cpu);
        coreId_np = (NumberPicker) this.findViewById(R.id.np_core);
        boxId_np.setMinValue(0);
        boxId_np.setMaxValue(31);
        slotId_np.setMinValue(0);
        slotId_np.setMaxValue(255);
        cpuId_np.setMinValue(0);
        cpuId_np.setMaxValue(255);
        coreId_np.setMinValue(0);
        coreId_np.setMaxValue(255);
        boxId_np.setBackgroundColor(Color.BLACK);  
        slotId_np.setBackgroundColor(Color.BLACK);  
        cpuId_np.setBackgroundColor(Color.BLACK);  
        coreId_np.setBackgroundColor(Color.BLACK);  
        scanBarCodeButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//打开扫描界面扫描条形码或二维码
				Intent openCameraIntent = new Intent(BarCodeTestActivity.this,CaptureActivity.class);
				startActivityForResult(openCameraIntent, 0);
			}
		});
        boxId_np.setOnValueChangedListener(npOnValueChangeListener);
        slotId_np.setOnValueChangedListener(npOnValueChangeListener );
        cpuId_np.setOnValueChangedListener(npOnValueChangeListener );
        coreId_np.setOnValueChangedListener(npOnValueChangeListener );
        /*ipStrEditText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				String str = ipStrEditText.getEditableText().toString();
				if(str!=null){
					CorePlaceInfo cpi = new CorePlaceInfo();
					
					cpi.setIpAddr(NetUtils.parseIp(str));
					boxId_np.setValue((int) cpi.getBoxId());
					slotId_np.setValue((int) cpi.getSlotId());
					cpuId_np.setValue((int) cpi.getDspId());
					coreId_np.setValue((int) cpi.getCoreId());
				}
			}
		});*/
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		//处理扫描结果（在界面上显示）
		if (resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			String scanResult = bundle.getString("result");
			resultTextView.setText(scanResult);
			snStrEditText.setText(getkey(scanResult));
		}
	}
	
	public String getkey(String mac) {
		if(mac==null)
			return null;
		mac = mac.replaceAll("-", "");
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			System.out.println(e.toString());

			e.printStackTrace();
		}
		char[] charArray = mac.toCharArray();

		byte[] byteArray = new byte[charArray.length];
		for (int i = 0; i < charArray.length; i++) {
			byteArray[i] = ((byte) charArray[i]);
		}
		byte[] md5Bytes = md5.digest(byteArray);
		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = md5Bytes[i] & 0xFF;
			if (val < 16) {
				hexValue.append("0");
			}
			hexValue.append(Integer.toHexString(val));
		}
		String key = hexValue.toString().toUpperCase();
		return key;
	}
}